Lumen & Gesso / website · lumengesso.com / app / api / orders / [id] / review / route.ts
| 1 | import { NextResponse, type NextRequest } from 'next/server'; |
| 2 | import { encodeReviewPhoto } from '@/app/lib/image-processing'; |
| 3 | import { reviewPhotoBucket } from '@/app/lib/review-photo-storage'; |
| 4 | import { commerceDb } from '@/app/lib/commerce-db'; |
| 5 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 6 | import { |
| 7 | REVIEWABLE_STATUSES, |
| 8 | REVIEW_COMMENT_LIMIT, |
| 9 | REVIEW_DISCOVERY_SOURCE_LIMIT, |
| 10 | REVIEW_DISCOVERY_SOURCES, |
| 11 | REVIEW_DISPLAY_NAME_LIMIT, |
| 12 | REVIEW_MAX_UPLOAD_BYTES, |
| 13 | } from '@/app/lib/reviews'; |
| 14 | |
| 15 | export const runtime = 'nodejs'; |
| 16 | export const dynamic = 'force-dynamic'; |
| 17 | |
| 18 | export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string }> }) { |
| 19 | const { id } = await ctx.params; |
| 20 | const user = await getCustomerUser(); |
| 21 | if (!user) return NextResponse.json({ error: 'Sign in to review your order.' }, { status: 401 }); |
| 22 | |
| 23 | const admin = commerceDb(); |
| 24 | const { data: order, error: orderError } = await admin.from('orders') |
| 25 | .select('status').eq('id', id).eq('user_id', user.id).maybeSingle(); |
| 26 | if (orderError) return NextResponse.json({ error: 'Could not load your order. Try again.' }, { status: 500 }); |
| 27 | if (!order) return NextResponse.json({ error: 'Order not found.' }, { status: 404 }); |
| 28 | if (!REVIEWABLE_STATUSES.includes(order.status)) { |
| 29 | return NextResponse.json({ error: 'Reviews open once your portrait goes to print.' }, { status: 409 }); |
| 30 | } |
| 31 | |
| 32 | const form = await req.formData().catch(() => null); |
| 33 | if (!form) return NextResponse.json({ error: 'Could not read your submission.' }, { status: 400 }); |
| 34 | const rating = Number(form.get('rating')); |
| 35 | const rawComment = form.get('comment'); |
| 36 | const rawName = form.get('display_name'); |
| 37 | const rawDiscovery = form.get('discovery_source'); |
| 38 | const rawOther = form.get('discovery_other'); |
| 39 | const comment = typeof rawComment === 'string' ? rawComment.trim() : ''; |
| 40 | const displayName = typeof rawName === 'string' ? rawName.trim() : ''; |
| 41 | const discoveryChoice = typeof rawDiscovery === 'string' ? rawDiscovery : ''; |
| 42 | const discoveryOther = typeof rawOther === 'string' ? rawOther.trim() : ''; |
| 43 | if (!Number.isInteger(rating) || rating < 1 || rating > 5) { |
| 44 | return NextResponse.json({ error: 'Choose a rating from 1 to 5 stars.' }, { status: 400 }); |
| 45 | } |
| 46 | if (!comment || comment.length > REVIEW_COMMENT_LIMIT || displayName.length > REVIEW_DISPLAY_NAME_LIMIT) { |
| 47 | return NextResponse.json({ error: 'Enter a comment and keep your name and comment within the listed limits.' }, { status: 400 }); |
| 48 | } |
| 49 | if ((rawDiscovery !== null && typeof rawDiscovery !== 'string') || (discoveryChoice && !REVIEW_DISCOVERY_SOURCES.includes(discoveryChoice))) { |
| 50 | return NextResponse.json({ error: 'Choose one of the options for how you heard about us.' }, { status: 400 }); |
| 51 | } |
| 52 | if (discoveryChoice === 'Other' && (!discoveryOther || discoveryOther.length > REVIEW_DISCOVERY_SOURCE_LIMIT)) { |
| 53 | return NextResponse.json({ error: `Tell us where you heard about us in ${REVIEW_DISCOVERY_SOURCE_LIMIT} characters or fewer.` }, { status: 400 }); |
| 54 | } |
| 55 | const discoverySource = discoveryChoice === 'Other' ? discoveryOther : discoveryChoice; |
| 56 | if (form.get('permission') !== 'on') { |
| 57 | return NextResponse.json({ error: 'Please confirm that we may display your review and any photo.' }, { status: 400 }); |
| 58 | } |
| 59 | |
| 60 | const { data: existing, error: lookupError } = await admin.from('order_reviews').select('id').eq('order_id', id).maybeSingle(); |
| 61 | if (lookupError) return NextResponse.json({ error: 'Could not check your review. Try again.' }, { status: 500 }); |
| 62 | if (existing) return NextResponse.json({ error: 'You’ve already reviewed this order.' }, { status: 409 }); |
| 63 | |
| 64 | const reviewId = crypto.randomUUID(); |
| 65 | const photo = form.get('photo'); |
| 66 | let photoPath: string | null = null; |
| 67 | if (photo instanceof File && photo.size > 0) { |
| 68 | if (photo.size > REVIEW_MAX_UPLOAD_BYTES) { |
| 69 | return NextResponse.json({ error: 'Please upload a smaller photo, up to 3 MB.' }, { status: 413 }); |
| 70 | } |
| 71 | let cleaned: Buffer; |
| 72 | try { |
| 73 | const image = await encodeReviewPhoto(Buffer.from(await photo.arrayBuffer())); |
| 74 | if (!image) { |
| 75 | return NextResponse.json({ error: 'Upload a JPG, PNG, or WebP photo.' }, { status: 400 }); |
| 76 | } |
| 77 | cleaned = image; |
| 78 | } catch { |
| 79 | return NextResponse.json({ error: 'That photo could not be read. Try another JPG, PNG, or WebP.' }, { status: 400 }); |
| 80 | } |
| 81 | photoPath = `${reviewId}.jpg`; |
| 82 | try { |
| 83 | await reviewPhotoBucket().put(photoPath, cleaned, { httpMetadata: { contentType: 'image/jpeg' } }); |
| 84 | } catch { |
| 85 | console.error('[order review] photo upload failed'); |
| 86 | return NextResponse.json({ error: 'Could not upload the photo. Try again.' }, { status: 502 }); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const { error } = await admin.from('order_reviews').insert({ |
| 91 | id: reviewId, order_id: id, rating, comment, display_name: displayName || null, photo_path: photoPath, |
| 92 | ...(discoverySource ? { discovery_source: discoverySource } : {}), |
| 93 | }); |
| 94 | if (error) { |
| 95 | if (photoPath && error.code === '23505') { |
| 96 | try { |
| 97 | await reviewPhotoBucket().delete(photoPath); |
| 98 | } catch { |
| 99 | console.error('[order review] duplicate photo cleanup failed'); |
| 100 | } |
| 101 | } |
| 102 | if (error.code === '23505') return NextResponse.json({ error: 'You’ve already reviewed this order.' }, { status: 409 }); |
| 103 | console.error('[order review] save failed', { code: error.code }); |
| 104 | return NextResponse.json({ error: 'Could not save the review. Try again.' }, { status: 500 }); |
| 105 | } |
| 106 | return NextResponse.json({ ok: true }, { status: 201 }); |
| 107 | } |